﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SnippetDoc.Entities;
using System.IO;

namespace SnippetDoc.Report
{
    public class HtmlReport
    {
        private string templateContent;

        public HtmlReport()
        {
            templateContent = File.ReadAllText("Template\\ReportTemplate.html");
        }

        public void CreateReport(string targetDirectory, IEnumerable<SnippetFolder> folders)
        {
            StringBuilder builder = new StringBuilder();

            foreach (var folder in folders)
            {
                builder.Append("<h2>" + folder.Name + "</h2>\n");
                builder.Append("<table width=\"100%\">\n");
                builder.Append(CreateTableHeader());
                builder.Append("<tbody>");
                folder.Snippets.Sort((a, b) => a.Shortcut.CompareTo(b.Shortcut));

                foreach (var snippet in folder.Snippets)
                {
                    builder.Append(CreateHtmlRepresentation(snippet) + "\n");
                }
                builder.Append("</tbody>");
                builder.Append("</table>\n");
            }

            templateContent = templateContent.Replace("$content$", builder.ToString());
            templateContent = templateContent.Replace("$generationDate$", DateTime.Now.ToString());

            WriteReportFiles(templateContent, targetDirectory);
        }

        private void WriteReportFiles(string reportContent, string targetDirectory)
        {
            string reportContentFolder = "snippetdoc";

            string endPath = Path.Combine(targetDirectory, reportContentFolder);

            if (Directory.Exists(endPath) == false)
            {
                Directory.CreateDirectory(endPath);
            }

            CopyResourceFilesToOutput(endPath);

            File.WriteAllText(endPath + "\\snippetdoc.html", reportContent);
        }

        private void CopyResourceFilesToOutput(string targetDirectory)
        {
            string sourceTemplateDirectory = "Template";

            foreach (var filename in Directory.EnumerateFiles(sourceTemplateDirectory))
            {
                if (filename.EndsWith(".js") || filename.EndsWith(".css"))
                    File.Copy(filename, targetDirectory + "\\" + filename.Replace(sourceTemplateDirectory + "\\", ""), true);
            }
        }

        private string CreateTableHeader()
        {
            return String.Format("<thead><th>{0}</th><th>{1}</th><th>{2}</th><th>{3}</th><th>{4}</th></thead>", "Title", "Shortcut", "Description", "Author", "Last Modified");
        }

        private string CreateHtmlRepresentation(Snippet snippet)
        {
            return String.Format(@"
              <tr onclick=""javascript:showCodeSample('{5}');"">
                <td class=""titleCol"">
                    {0}
                    <div id=""{5}"" style=""display:none""><pre class=""prettyprint"">{6}</pre></div>
                </td>
                <td class=""shortcutCol"">{1}</td>
                <td class=""descCol"">{2}</td>
                <td class=""authorCol"">{3}</td>
                <td class=""lastModifiedCol"">{4}</td>
              </tr>", snippet.Title, snippet.Shortcut, snippet.Description, snippet.Author, snippet.LastModified.ToString(), Guid.NewGuid().ToString().Replace("-", ""), Encode(snippet.CodeSample));
        }

        private string Encode(string p)
        {
            return p.Replace("<", "&lt;");
        }
    }
}
